package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Api(tags = "C端地址簿相关接口")
@RequestMapping("/user/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;
    //新增地址
    @PostMapping
    public Result add(@RequestBody AddressBook addressBook){
        addressBookService.add(addressBook);
        return Result.success();
    }

    //查询当前登录用户的所有地址信息
    @GetMapping("/list")
    public Result<List<AddressBook>> list(){
        List<AddressBook> addressBookList=addressBookService.list();
        return Result.success(addressBookList);
    }

    //设置默认地址
    @PutMapping("/default")
    public Result setDefault(@RequestBody AddressBook addressBook){
        addressBookService.setDefault(addressBook);
        return Result.success();
    }

    //查询默认地址
    @GetMapping("/default")
    public Result<AddressBook> getDefault(){
        AddressBook addressBook=addressBookService.getDefault();
        return Result.success(addressBook);
    }

    //根据id查询地址
    @GetMapping("/{id}")
    public Result<AddressBook> getAddressById(@PathVariable Long id){
        AddressBook addressBook=addressBookService.getAddressById(id);
        return Result.success(addressBook);
    }

    //根据id修改地址
    @PutMapping
    public Result UpdateAddressById(@RequestBody AddressBook addressBook){
        addressBookService.UpdateAddressById(addressBook);
        return Result.success();
    }

    //根据id删除地址
    @DeleteMapping
    public Result deleteAddressById(Long id){
        addressBookService.deleteAddressById(id);
        return Result.success();
    }
}
